package org.shagin.jfin.data;

import java.util.Currency;

import org.shagin.jfin.data.util.CurrencyUtil;

/**
 * The Class Money.
 */
public class Money {

    private Currency currency;
    private double value = 0d;

    /**
     * Instantiates a new money.
     * 
     * @param currency the currency
     * @param value the value
     */
    public Money(Currency currency, double value) {
        this.currency = currency;
        this.value = value;
    }

    /**
     * Gets the value.
     * 
     * @return the value
     */
    public double getValue() {
        return value;
    }

    /**
     * Sets the value.
     * 
     * @param value the new value
     */
    public void setValue(double value) {
        this.value = value;
    }

    /**
     * Gets the currency.
     * 
     * @return the currency
     */
    public Currency getCurrency() {
        return currency;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return CurrencyUtil.format(currency, value);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((currency == null) ? 0 : currency.hashCode());
        long temp;
        temp = Double.doubleToLongBits(value);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        return result;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Money other = (Money) obj;
        if (currency == null) {
            if (other.currency != null)
                return false;
        } else if (!currency.equals(other.currency))
            return false;
        if (Double.doubleToLongBits(value) != Double
                .doubleToLongBits(other.value))
            return false;
        return true;
    }

    /**
     * Sets the currency.
     * 
     * @param currency the new currency
     */
    public void setCurrency(Currency currency) {
        this.currency = currency;
    }
}
